package pcjavanet.mobilesell.service;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;  
import java.util.Iterator; 
import java.util.Map;      
import pcjavanet.mobilesell.model.Employee;
import pcjavanet.mobilesell.model.EmployeeExample;
import pcjavanet.mobilesell.model.Logrecord; 
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.LogrecordExample;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;
import pcjavanet.mobilesell.persistence.EmployeeMapper;
import pcjavanet.mobilesell.persistence.LogrecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class LogrecordService {


  @Autowired
  private  LogrecordMapper logrecordMapper;
  @Autowired
  private  EmployeeMapper employeeMapper;


  public int  insert( Logrecord logrecord ) throws Exception{
	return logrecordMapper.insert (logrecord);
  }


  public Logrecord  selectByPrimaryKey( Logrecord  bean ) {
 	int dbid = bean.getDbid();
	Logrecord logrecord = logrecordMapper.selectByPrimaryKey( dbid );
	Employee employee = employeeMapper.selectByPrimaryKey( logrecord.getRefEmployeeDbid() );
	logrecord.setEmployee(employee) ;
	return logrecord ;
  }


  public int  updateByPrimaryKeySelective( Logrecord logrecord ) throws Exception{
	return logrecordMapper.updateByPrimaryKeySelective( logrecord );
  }


  public int  deleteByPrimaryKey( Logrecord  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return logrecordMapper.deleteByPrimaryKey( dbid );
  }


  public List<Logrecord>  selectByExample( Logrecord logrecord ) throws Exception{
    LogrecordExample example = new LogrecordExample();
	return logrecordMapper.selectByExample( example );
  }

  public List<Logrecord>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    LogrecordExample example = new LogrecordExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<Logrecord> ls=logrecordMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> employeeIdmap = new HashMap<Integer,Integer>();
  		for(Logrecord  m : ls){
  			m.setEmployee(new Employee ()) ; 
  			if ( m.getRefEmployeeDbid() != null ) 
  				employeeIdmap.put(m.getRefEmployeeDbid(), m.getRefEmployeeDbid());
  		};
  		List<Integer> refEmployeeDbids = new ArrayList<Integer>();
  		Iterator<Integer> refEmployeeDbidit = employeeIdmap.keySet().iterator(); 
  		while ( refEmployeeDbidit.hasNext() ){
  			refEmployeeDbids.add( refEmployeeDbidit.next() );
  		}
  		EmployeeExample employeeExample = new EmployeeExample();
  		if ( refEmployeeDbids.size() > 0 ) 
  			employeeExample.createCriteria().andDbidIn( refEmployeeDbids );
  		List<Employee> employees = employeeMapper.selectByExample( employeeExample);
  		for(Logrecord m : ls ){
  			for(Employee c : employees ){
  				if ( m.getRefEmployeeDbid() !=null && c.getDbid() !=null && m.getRefEmployeeDbid().intValue() == c.getDbid().intValue() ) {
  					m.setEmployee( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    LogrecordExample example = new LogrecordExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return logrecordMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Logrecord logrecord , PageBean pageBean ){
    LogrecordExample example = new LogrecordExample();
	return logrecordMapper.countByExample( example );
  }
  public List<Logrecord> selectByExamplePage( Logrecord logrecord , PageBean pageBean ){
  LogrecordExample example = new LogrecordExample();
	return logrecordMapper.selectByExamplePage( example , pageBean );
  }

}
